package web

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"testPaper/common"
	"testPaper/model"
	"testPaper/response"
)

// CreateNewTopic 添加真题
func CreateNewTopic(c *gin.Context) {
	DB := common.GetDB()
	paperId := c.PostForm("paperId")
	subject := c.PostForm("subject")
	choose := c.PostForm("choose")
	img := c.PostForm("img")
	types := c.PostForm("type")
	rightKey := c.PostForm("rightKey")
	adminPhone := c.PostForm("adminPhone")
	caseId := c.PostForm("caseId")

	if len(paperId) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "试卷id不能为空")
		return
	} else if len(subject) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "题目不能为空")
		return
	} else if len(choose) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "选择项不能为空值")
		return
	} else if len(rightKey) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "正确答案和题目说明不能为空")
		return
	} else if len(types) == 0 {
		response.Reponse(c, http.StatusOK, 422, nil, "题目类型不能为空")
		return
	}
	if CaseExits(DB, paperId) {
		newTopic := model.NewTopic{
			Subject:    subject,
			Choose:     choose,
			Img:        img,
			AdminPhone: adminPhone,
			PaperId:    paperId,
			RightKey:   rightKey,
			Type:       types,
			CaseId:     caseId,
		}
		DB.Create(&newTopic)
		response.Reponse(c, http.StatusOK, 200, nil, "创建成功")
		return
	} else {
		response.Reponse(c, http.StatusOK, 422, nil, "传入的试卷Id不存在")
		return
	}
}
